package com.lysh.simple;

import okhttp3.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liyishan
 * @date 2023/8/4 15:30
 * @apiNote
 */

public final class HttpUtils {
    private static final OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS).build();

    private HttpUtils() {
    }

    /**
     * 同步POST&JSON
     *
     * @param url  url
     * @param json json
     * @return String
     * @throws IOException IOException
     */
    public static String postJson(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Request request = new Request.Builder().url(url).post(body)
                .addHeader("Content-Type", "application/json")
                .build();

        Response response = execute(request);
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
    /**
     * OkHttpClient POST
     *
     * @param url   地址
     * @param param 参数
     * @return 结果
     * @throws IOException IO异常
     */
    public static String okPost(String url, Map<String, String> param) throws Exception {
        //01 构建参数
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : param.keySet()) {
            builder.add(key, URLEncoder.encode(param.get(key), "UTF-8"));
        }
        RequestBody body = builder.build();
        //组织请求
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();

        //发起请求
        Response response = execute(request);
        if (response.isSuccessful()) {
            return response.body() != null ? response.body().string() : null;
        } else {
            throw new Exception("IOException: Unexpected code:" + response);
        }
    }


    /**
     * 同步GET
     *
     * @param url url
     * @return String
     * @throws IOException IOException
     */
    public static String get(String url) throws IOException {
        Request request = new Request.Builder().url(url).get().build();
        Response response = execute(request);
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }


    /**
     * 同步GET
     *
     * @param url url
     * @return String
     * @throws IOException IOException
     */
    public static String getToWanMeiBaiHuo(String url,String ssoSessionId) throws IOException {
        Request request = new Request.Builder().url(url).get().addHeader("ssoSessionId",ssoSessionId).build();
        Response response = execute(request);
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }



    //-----------------------------------------------------------------------------------


    /**
     * 通用同步请求
     *
     * @param request 请求
     * @return {@link Response}
     * @throws IOException IOException
     */
    public static Response execute(Request request) throws IOException {
        return okHttpClient.newCall(request).execute();
    }

    /**
     * 通过token发送请求获取信息
     *
     * @param url       下游接口地址
     * @param token     下游传递过来
     * @param oathToken 请求下游地址获取的token(用于加在请求头)
     * @return 用户信息
     * @throws IOException
     */
    public static String postJson2(String url, String token, String oathToken) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), token);
        Request request = new Request.Builder().url(url).post(body).addHeader("Authorization", "bearer " + oathToken).build();
        Response response = execute(request);
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * 通过token发送请求获取信息
     *
     * @param url       下游接口地址
     * @param token     下游传递过来
     * @param oathToken 请求下游地址获取的token(用于加在请求头)
     * @return 用户信息
     * @throws IOException
     */
    public static String postJsonWanMeiBaiHuo(String url, String token, String oathToken) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), token);
        Request request = new Request.Builder().url(url).post(body).addHeader("Authorization", "bearer " + oathToken).build();
        Response response = execute(request);
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }


}
